﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daco.Web.Store;
using Daco.Web.Controls;

namespace Daco.Web.Pages.EStore
{
    public partial class View : Daco.Web.UI.SitePage
    {
        protected CProduct _currentProduct;

        public View()
        {
            this.Init += new EventHandler(View_Init);
            this.Load += new EventHandler(View_Load);
        }

        void View_Init(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "JQuery.UI.Tabs", Page.ResolveUrl("~/Scripts/jquery.ui.tabs.min.js"));
        }

        void View_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["pid"]) &&
                (_currentProduct = CProduct.GetProduct(Request.QueryString["pid"])) != null)
            {
                // Get product details
                iProductPhoto.Src = String.Format("{0}?pid={1}", GetPageUrl("~/Handlers/PhotoHandler.ashx"), _currentProduct.GetDefaulPhoto().PhotoID);
                lProductName.Text = _currentProduct.ProductName;
                lProductSeries.InnerText = _currentProduct.Series;
                lProductCategoryTitle.InnerText = _currentProduct.ProductCategory.Title;
                lProductManufactureName.InnerText = _currentProduct.Manufacture.Name;

                if (!_currentProduct.EnablePriceConfidential && _currentProduct.UnitPrice > 0)
                    lProductPrice.InnerText = string.Format("{0} {1} / {2}", _currentProduct.UnitPrice.ToString(Config.CSiteConfig.Current.DoubleFormatString), _currentProduct.Currency, _currentProduct.UnitMeasure);
                else
                    lProductPrice.InnerText = "Liên hệ";

                List<CProductDescription> descriptions = _currentProduct.GetDescriptions();
                rpDescription.DataSource = descriptions;
                rpDescription.DataBind();

                if (descriptions.Count > 0)
                {
                    CProductDescription defaultDescription = _currentProduct.GetDescription(descriptions[0].DescriptionID);
                    lDescription.Text = defaultDescription.Body;
                }
                
                if (Daco.Web.Config.CSiteConfig.Current.EnableContactFromProduct)
                {
                    plhControl.Controls.Add(Page.LoadControl("~/Controls/ContactForm.ascx"));
                }

                // Lay danh sach cac description cua product

                //Display releated products
                RelatedProduct ctrlRelatedProduct = (RelatedProduct)Page.LoadControl("~/Controls/RelatedProduct.ascx");
                ctrlRelatedProduct.Product = _currentProduct;
                plhControl.Controls.Add(ctrlRelatedProduct);

                // Register productID field
                Page.ClientScript.RegisterHiddenField("ctrlProductID", _currentProduct.ProductID);

                // Setup page title
                Page.Title = _currentProduct.ProductName + " - " + Page.Title;
            }
            else
                CLinkBuilder.AccessDenied();   
        }
    }
}